package com.yz.study.myjunit;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.yz.study.annotations.YzTest;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;


    @Slf4j
    public class AutoTestClient {
        public static void main(String[] args) throws
                IOException, ClassNotFoundException, NoSuchMethodException,
                InvocationTargetException, InstantiationException, IllegalAccessException {
            CalcHelpDemo calcHelpDemo = new CalcHelpDemo();
            int para1 = 10;
            int para2 = 0;


            AtomicInteger bugCount = new AtomicInteger();
            // 要写入的文件路径（如果文件不存在，会创建该文件）
            String filePath = "BugReport" + (DateUtil.format(new Date(), "yyyyMMddHHmmss")) + ".txt";

            Method[] methods = calcHelpDemo.getClass().getMethods();


            for (int i = 0; i < methods.length; i++) {
                if (methods[i].isAnnotationPresent(YzTest.class)) {
                    try {
                        methods[i].invoke(calcHelpDemo, para1, para2);


                    } catch (Exception e) {
                        bugCount.getAndIncrement();//i++
                        log.info("异常名称:{},异常原因:{}", e.getCause().getClass().getSimpleName(), e.getCause().getMessage());

                        FileUtil.writeString(methods[i].getName() + "\t" + "出现了异常" + "\n", filePath, "UTF-8");
                        FileUtil.appendString("异常名称:" + e.getCause().getClass().getSimpleName() + "\n", filePath, "UTF-8");
                        FileUtil.appendString("异常原因:" + e.getCause().getMessage() + "\n", filePath, "UTF-8");
                    } finally {
                        FileUtil.appendString("异常数:" + bugCount.get() + "\n", filePath, "UTF-8");
                        System.out.println("bug报告路径: " + "target/classes/XXXXXXX.txt");
                    }
                }
            }
        }
    }

